﻿using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.IO;
using TouchRC.MetadataCaching.Extensions;
using System.Xml;

namespace MediaScoutGUI.GUITypes
{
    public class TVShow : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        
        private bool m_HasMetadata = false;
        private bool m_HasPoster = false;
        private bool m_HasBackdrop = false;
        private Int32 m_Id;
        private DirectoryInfo m_Filepath;
        private string m_Name;
        private string m_Overview;
        
        public string Overview
        {
            get { return m_Overview; }
            set { m_Overview = value; }
        }

        List<Season> m_Seasons;

        public bool HasPoster
        {
            get { return m_HasPoster; }
            set
            {
                m_HasPoster = value;
                NotifyPropertyChanged("HasPoster");
            }
        }

        public bool HasBackdrop
        {
            get { return m_HasBackdrop; }
            set
            {
                m_HasBackdrop = value;
                NotifyPropertyChanged("HasBackdrop");
            }
        }

        public List<Season> Seasons
        {
            get { return m_Seasons; }
            set { m_Seasons = value; }
        }

        public bool HasMetadata
        {
            get { return m_HasMetadata; }
            set { m_HasMetadata = value; }
        }
        public Int32 Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }
        public DirectoryInfo Filepath
        {
            get { return m_Filepath; }
            set { m_Filepath = value; }
        }
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        public TVShow(DirectoryInfo filepath)
        {
            FileInfo seriesFile = filepath.GetTvSeriesXml();

            if (File.Exists(seriesFile.FullName))
            {
                this.m_Filepath = filepath;
                this.m_Name = filepath.Name;

                m_HasMetadata = true;
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(seriesFile.FullName);
                XmlNode node = xdoc.DocumentElement;

                m_Id = Int32.Parse(node.SelectSingleNode("id").InnerText);

                m_Name = node.SelectSingleNode("SeriesName").InnerText;

                if (node.SelectSingleNode("Overview") != null)
                    m_Overview = node.SelectSingleNode("Overview").InnerText;

                m_Seasons = new List<Season>();

                foreach (DirectoryInfo di in filepath.GetDirectories())
                {
                    if (di.Name != "metadata")
                    {
                        GUITypes.Season s = new GUITypes.Season(di.FullName, di.Name, this);
                        m_Seasons.Add(s);
                    }
                }

                if (File.Exists(m_Filepath.GetFolderPoster().FullName))
                    m_HasPoster = true;
                if (File.Exists(m_Filepath.GetFolderBackdrop().FullName))
                    m_HasBackdrop = true;
            }
        }

        public override String ToString()
        {
            return this.Name;
        }
    }
}
